import { track, trigger } from "./effect.js"
import { isObject } from "../shared/index.js"
import { reactive } from "./reactive.js"

export function ref(val) {
  return createRef(val)
}

function createRef(val) {
  if (isRef(val)) {
    return val
  }
  return new RefImpl(val)
}

function isRef(val) {
  return !!(val && val.__v_isRef)
}


class RefImpl {
  constructor(val) {
    this._val = convert(val)// 原始类型
    this.__v_isRef = true
  }

  get value() {
    track(this, 'get', 'value')
    return this._val
  }

  set value(newVal) {
    this._val = newVal
    // 视图更新
    trigger(this, 'set', 'value') // 触发依赖
  }

}

function convert(val) {
  return isObject(val) ? reactive(val): val
}